﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;
using System.Collections;
using System.Collections.Generic;

namespace Model
{
    [Serializable()]
    public class PhieuXuatChiTietBOCollection : Csla.BusinessListBase<PhieuXuatChiTietBOCollection, PhieuXuatChiTietBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            PhieuXuatChiTietBO _PhieuXuatChiTietBO = PhieuXuatChiTietBO.NewChildPhieuXuatChiTietBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_PhieuXuatChiTietBO));
            _PhieuXuatChiTietBO.STT0 = this.Count + 1;
            Add(_PhieuXuatChiTietBO);
            return _PhieuXuatChiTietBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private PhieuXuatChiTietBOCollection()
        {
        }

        public static PhieuXuatChiTietBOCollection NewPhieuXuatChiTietBOCollection()
        {
            return new PhieuXuatChiTietBOCollection();
        }

        public static PhieuXuatChiTietBO NewPhieuXuatChiTietBO()
        {
            return PhieuXuatChiTietBO.NewChildPhieuXuatChiTietBO();
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection()
        {
            SPParam spParam = new SPParam("PhieuXuatChiTietID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("PhieuXuatChiTietID", "ASC", "");
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where)
        {
            SPParam spParam = new SPParam("PhieuXuatChiTietID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("PhieuXuatChiTietID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhieuXuatChiTietID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhieuXuatChiTietID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhieuXuatChiTietID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }

        public static PhieuXuatChiTietBOCollection GetPhieuXuatChiTietBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhieuXuatChiTietID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhieuXuatChiTietBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuXuatChiTietBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    PhieuXuatChiTietBO objPhieuXuatChiTietBO = PhieuXuatChiTietBO.GetChildPhieuXuatChiTietBO(dr);
                    objPhieuXuatChiTietBO.STT0 = ++stt;
                    this.Add(objPhieuXuatChiTietBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            LogBOCollection logBOCollection = LogBOCollection.NewLogBOCollection();

            RaiseListChangedEvents = false;
            PhieuXuatChiTietBO child = null;
            int length = this.Count;
            int stt = 0;
            bool del = false;
            int i = 0;
            for (i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                {
                    child.Insert();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "PhieuXuatChiTietID", TacVu.Them, "PhieuXuatChiTiet", child.PhieuXuatChiTietID.Value));
                }
                else
                {
                    child.Update();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "PhieuXuatChiTietID", (child.DELE.Value ? TacVu.Xoa : TacVu.Sua), "PhieuXuatChiTiet", child.PhieuXuatChiTietID.Value));
                }

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
                else
                {
                    child.STT0 = ++stt;
                }
            }
            RaiseListChangedEvents = true;

            //-----
            logBOCollection = logBOCollection.Save();
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public PhieuXuatChiTietBO GetPhieuXuatChiTietBO(int _PhieuXuatChiTietID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].PhieuXuatChiTietID == _PhieuXuatChiTietID)
                    return this[i];

            return null;
        }

        public void Remove(int PhieuXuatChiTietID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].PhieuXuatChiTietID == PhieuXuatChiTietID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }

        public void ImportHoaDonBanChiTiet(HoaDonBanChiTietBOCollection _HoaDonBanChiTietBOCollection)
        {
            int length = _HoaDonBanChiTietBOCollection.Count;
            for (int i = 0; i < length; i++)
            {
                PhieuXuatChiTietBO newBO = PhieuXuatChiTietBO.NewRootPhieuXuatChiTietBO();
                newBO.MatHangID = _HoaDonBanChiTietBOCollection[i].MatHangID.Value;
                newBO.TenHang = _HoaDonBanChiTietBOCollection[i].TenHang;
                newBO.DonViTinhID = _HoaDonBanChiTietBOCollection[i].DonViTinhID.Value;
                newBO.TenDonViTinh = _HoaDonBanChiTietBOCollection[i].TenDonViTinh;
                newBO.DonGia = _HoaDonBanChiTietBOCollection[i].DonGia.Value;
                newBO.SoLuong = _HoaDonBanChiTietBOCollection[i].SoLuong.Value;
                newBO.ThanhTien = _HoaDonBanChiTietBOCollection[i].ThanhTien.Value;
                if (_HoaDonBanChiTietBOCollection[i].DanhMucKhoID != null)
                    newBO.DanhMucKhoID = _HoaDonBanChiTietBOCollection[i].DanhMucKhoID.Value;
                newBO.GhiChu = _HoaDonBanChiTietBOCollection[i].GhiChu;

                this.Add(newBO);
            }
        }

        #endregion Business Method
        //==========================================================================
    }
}
